/*
 * Created by SharpDevelop.
 * User: rseward
 * Date: 6/4/2008
 * Time: 2:23 PM
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System;
using System.Text;
using System.Collections.Generic;

using RD.Metadata.Database.Sql;

using AvvaMobileNHibernateHelperKit.Framework;

namespace AvvaMobileNHibernateHelperKit
{
	/// <summary>
	/// This method generates CSharp Class code CSharpClassGenerator.
	/// </summary>
	public class CSharpClassGenerator : BaseClassCodeGenerator
	{
		
		
		public CSharpClassGenerator(MainForm form, Data dataClass, Project project) : base(form, dataClass, project)
		{
			//this.form = form;
			//this.dataClass = dataClass;
			//this.project = project;			
		}
		
		public string FormatFieldDeclaration(Dictionary<string,string> fieldDecl)
		{
			string fieldDeclaration = fieldDecl[ "fieldPrefix" ]  + " " + fieldDecl[ "fieldType"] + " " + fieldDecl[ "fieldName" ] + ";" ;
			
			return fieldDeclaration;
		}
		
		public string FormatPropertyDeclaration(Dictionary<string,string> fieldDecl)
		{
			StringBuilder buf = new StringBuilder( 200 );
			
			String crlf = Constants.CRLF;
			
			buf.Append( fieldDecl[ "propPrefix" ]  + " virtual " + fieldDecl["fieldType"] +" " +fieldDecl["propName"] + " {"+ crlf );
			buf.Append( "  get { return "+ fieldDecl["fieldName"]+"; }" + crlf );
			buf.Append( "  set { "+ fieldDecl["fieldName"] + " = value; }"+ crlf );
			buf.Append( "}"+ crlf );
			
			return ""+buf;
		}
		
		
		public override void GenerateClasses()
		{
            #region Write Classes

                form.Progress("Persistent classes generating", 35);
                
                foreach (string tableName in project.SelectedTables) 
                {
                	string className = project.NameFormatter.GetClassName(tableName) ;
                	
                	System.IO.StreamWriter classSw = new System.IO.StreamWriter(project.ProjectDir + @"\App_Code\" + className + ".cs");
                	
                	classSw.WriteLine("// "+GeneratedByText);
                	classSw.WriteLine("");
                	classSw.WriteLine("using System;");
                	classSw.WriteLine("");
                	classSw.WriteLine("namespace " + project.Namespace);
                	classSw.WriteLine("{");
                	classSw.WriteLine("public class " + className);
                	classSw.WriteLine("{");

                	form.Progress("Capturing table columns schema", 0);
                	
                	SqlMetaTable tabMeta = dataClass.GetTableInfo( tableName );
                	
                	if (tabMeta == null) {
                		throw new Exception("Unable to find meta data for "+tableName);
                	}
                	
                	
                	form.Progress("Writing fields and properties for Class " + className , 0);
                	#region Writing Fields and Properties
                	List<string> fieldDecls = new List<string>();
                	List<string> propDecls = new List<string>();
                	
                	foreach (SqlMetaColumn col in tabMeta.Columns)
                	{
                		string colName = col.ColumnName;
                		string fieldName = project.NameFormatter.GetFieldName( className, col.ColumnName);
                		string propName = project.NameFormatter.GetPropertyName( className, colName);
                		
                		string fieldDecl = FormatFieldDeclaration( project.DbDriver.GetClassFieldDeclaration(project, className, col, fieldName) );
                		string propDecl = FormatPropertyDeclaration( project.DbDriver.GetClassPropertyDeclaration(project, className, col, fieldName, propName) );
                		
                		fieldDecls.Add( fieldDecl );
                		propDecls.Add( propDecl );
                		
                	}
                	#endregion
                	
                	foreach (string fieldDecl in fieldDecls) {
                		classSw.WriteLine( fieldDecl );
                	}
                	classSw.WriteLine("");
                	classSw.WriteLine("");
                	
                	foreach (string propDecl in propDecls) {
                		classSw.WriteLine( propDecl );
                	}
                	classSw.WriteLine("");
                	classSw.WriteLine("");
                	
                	form.Progress("Fields and properties applied for Class " + className, 0);

                	classSw.WriteLine("");
                	classSw.WriteLine("}");
                	classSw.WriteLine("}");
                	classSw.Flush();
                	classSw.Close();
                	classSw.Dispose();
                	form.Progress("Class file " + className + " generated" , 0);
                }

                form.Progress("Persistent classes generated", 60);

                #endregion      
			
		}
	}
}
